<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
      default-expand-all
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-button size="small">取消</el-button>
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
    </el-row>
  </div>
</template>

<script>
import { getRoleDetail, assignPerm } from '@/api/settings'
import { getPermissionList } from '@/api/permisson'
import { arraytoTree } from '@/utils/index'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
    this.loadPermissionByRoleId()
  },
  methods: {
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await assignPerm({ id: this.roleId, permIds })
      console.log(res)
      this.$emit('close-dialog')
    },
    async loadPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      this.list = arraytoTree(res.data)
    },
    async loadPermissionByRoleId() {
      const res = await getRoleDetail(this.roleId)
      console.log('权限管理', res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    }
  }
}
</script>

<style></style>
